if( hq == undefined ) {
	var hq = {};
}

hq.map = {
	
	gMap : null,

        suspendSessionSave : null,
	
	init : function() {
		console.group( 'map::init' );
		
		if( GBrowserIsCompatible() == false ) {
			throw new Error( "This browser is not compatible with Google Maps." );
		}
		
		this.gMap = new GMap2( $j( '#map' )[0] );
		var center = $j('#center').val();
		if(!center) center = "47.607088939995585, -122.32933044433594"; //$j('#mapCenter').val();
		var zoom = $j('#zoom').val();
                if(!zoom)  zoom = 13; //parseInt($j('#mapZoom').val());

                center = center.split(',');
//		center[0] = parseFloat(center[0]);
//		center[1] = parseFloat(center[1]);
                // While the map does get recentered during the initial pane processing, removing this causes google maps to crash.
                this.gMap.setCenter( new GLatLng( parseFloat(center[0]), parseFloat(center[1]) ), parseInt(zoom) );
//		this.gMap.setZoom(parseInt(zoom));

                this.gMap.addControl( new GLargeMapControl() );
		this.gMap.addControl( new GMapTypeControl() );
		this.gMap.addControl( new GScaleControl() );
		this.gMap.addControl( new GOverviewMapControl() );
		
		this.gMap.enableContinuousZoom();
		this.gMap.enableScrollWheelZoom();
		this.gMap.enableInfoWindow();
		
		var $pane = $j( '#hq_idx_content' );
	
		this.mkt = $pane.attr( 'Market' );	
		
		var activeLayer = $j('.activeLayer').attr('id');

		this.hideLoading = true;

		var mapType = $j('#hq_idx_content').attr('maptype');
		var mapConst = null;
		if(mapType) {
			switch(mapType) {
				case 'Satellite' :
					mapConst = G_SATELLITE_MAP;
					break;
				case 'Hybrid' :
					mapConst = G_HYBRID_MAP;
					break;
				default :
					mapConst = G_NORMAL_MAP;
			}

			hq.map.gMap.setMapType( mapConst )

		}

		this.clickCount = 0;

		var m = this;
		$j( '#mapWrapper' ).addClass( 'evtBound' ).resize( toolbox.delegate( this.resize, this ) )
		.click(function() {
			m.clickCount++;
		});
		$j( '#contentWrapper' )
			.bind( 'postPaneLoad', toolbox.delegate( this.onPostPaneLoad, this ) );

		$j( window ).unload( toolbox.delegate( this.unload, this ) );
		
		this.onPostPaneLoad();
		
		console.groupEnd();
	},
	
	resize : function() {
		console.group( 'map::resize' );
		
		this.gMap.checkResize();
		this.onPostPaneLoad();
		console.groupEnd();
	},
	
	unload : function() {
		
		GUnload();
		
	},
	
	onPostPaneLoad : function( evt ) {
		console.group( 'map::onPostPaneLoad' );

		var $pane = $j( '#hq_idx_content' );
		var yMin = $pane.attr( 'yMin' );
		var yMax = $pane.attr( 'yMax' );
		var xMin = $pane.attr( 'xMin' );
		var xMax = $pane.attr( 'xMax' );
		var $spaceType = $j('#curSpaceType').val();
		var doZoom = $j('#doZoom').val();
		var loadedSpaces = $j('#loadedSpaceIds').val();

		if(loadedSpaces)
			loadedSpaces = loadedSpaces.split(',');

		if(this.clickCount < 2  && (!loadedSpaces || loadedSpaces.length==1)) {
			doZoom = true;
			$j('#doZoom').val('');	
		}

		if( yMin && yMax && xMin && xMax ) 
		{
			var bounds = new GLatLngBounds();
			bounds.extend( new GLatLng( yMin, xMin, true ) );
			bounds.extend( new GLatLng( yMax, xMax, true ) );

			if(($spaceType!=4 && $spaceType!=3 && $spaceType!='') || doZoom) {
			hq.map.gMap.setCenter( bounds.getCenter() );

			if(doZoom) 
				 hq.map.gMap.setZoom(  hq.map.gMap.getBoundsZoomLevel( bounds ) );
			else 
				 hq.map.gMap.setZoom( 10 );
			}
		}

		if(this.hideLoading) {
			this.hideLoading = false;
//			hq.loadingMsg.hide();
		}
		

		console.groupEnd();
	},
	
	getBounds : function() {
		console.group( 'map::getBounds' );
		
		var bounds = hq.map.gMap.getBounds();
		var ne = bounds.getNorthEast();
		var sw = bounds.getSouthWest();
		
		var res = {
			minX : ne.lng(),
			maxX : sw.lng(),
			minY : sw.lat(),
			maxY : ne.lat()
		};
		
		console.groupEnd();

		return res;
	}


	
};

$j( document ).ready( toolbox.delegate( hq.map.init, hq.map ) );
